﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using mediaPrime.Sdk;
using mediaPrime.TVServerProvider;

namespace mediaPrime.Plugin.TV
{
    public class TVPlugin : mediaPrime.Sdk.Plugin
    {
        private MainUserInterface mainUserInterface;
        private TileContent tileContent;
        private bool initialized = false;

        public List<ExtensionInfo> TVServerProviders { get; set; }
        public TVServerProviderBase SelectedTVServerProvider { get; set; }

        public TVPlugin()
        {
            mainUserInterface = new MainUserInterface(this);

            this.CustomSettingDisplayNameRequest += TVPlugin_CustomSettingDisplayNameRequest;
            this.CustomSettingDialogRequest += TVPlugin_CustomSettingDialogRequest;

            this.Show += TVPlugin_Show;
            this.Hide += TVPlugin_Hide;

            TVServerProviders = new List<ExtensionInfo>();
            TVServerProviders.AddRange(AvailableAddons.Where(a => a.AddonType == "TVServerProvider"));
        }

        void TVPlugin_Hide(object sender, System.EventArgs e)
        {

        }

        void TVPlugin_Show(object sender, System.EventArgs e)
        {
            if (!initialized)
                initTVPlugin();
        }

        private void initTVPlugin()
        {
            initTVServerProvider();

            initialized = true;
        }

        private void initTVServerProvider()
        {
            var tvServerProviderSetting = Settings.UserSettings["TVServerProvider"].GetValue<string>(null);
            if (!string.IsNullOrEmpty(tvServerProviderSetting))
            {
                var tvServerProvider = TVServerProviders.Where(t => t.Id == tvServerProviderSetting).FirstOrDefault();
                if (tvServerProvider != null)
                {
                    try
                    {
                        SelectedTVServerProvider = TVServerProviderManager.LoadTVServerProvider(tvServerProvider.Path);
                    }
                    catch (Exception ex)
                    {
                        // TODO
                    }
                }
            }

            // TODO: What if no tv server provider is selected
        }

        private CustomSettingsDialog TVPlugin_CustomSettingDialogRequest(object sender, Sdk.SettingEventArgs e)
        {
            if (e.Setting.Type == "TVServerProviderType")
                return new UserControls.TVServerProviderSettingsDialog(this);

            return null;
        }

        private string TVPlugin_CustomSettingDisplayNameRequest(object sender, Sdk.SettingEventArgs e)
        {
            if (e.Setting.Type == "TVServerProviderType")
                return TVServerProviders.Where(t => t.Id == e.Setting.Value).Select(t => t.Name).FirstOrDefault();

            return null;
        }

        public override System.Windows.UIElement GetUserInterface()
        {
            return mainUserInterface;
        }

        public override UIElement GetTileContent()
        {
            if (tileContent == null)
                tileContent = new TileContent();

            return tileContent;
        }
    }
}
